#!/bin/bash
FFMPEG_VERSION=$1
if [ -z ${FFMPEG_VERSION} ];then
  FFMPEG_VERSION=4.1.8
fi
FFMPEG_URL=https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n${FFMPEG_VERSION}.zip
function download_and_install(){
  url=$1
  ffmpeg_install_dir=$2
  filename=`basename ${url}`
  temp_dir=/tmp/${basename}
  if [ -f "${temp_dir}" ];then
      wget -c ${url} -O ${temp_dir}
  fi
  if [ -z ${ffmpeg_install_dir} ];then
    ffmpeg_install_dir="/usr/local/ffmpeg"
  fi
  
  unzip ${temp_dir} -d /tmp
  pushd -d /tmp/${version}
  export PKG_CONFIG_PATH="${ffmpeg_install_dir}/lib/pkgconfig:$PKG_CONFIG_PATH"
  ./configure --prefix="${ffmpeg_install_dir}" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I${ffmpeg_install_dir}/include -fPIC" \
  --extra-ldflags="-L${ffmpeg_install_dir}/lib -Wl,-Bsymbolic" \
  --extra-libs="-lpthread -lm" \
  --bindir="/usr/local/bin" \
  --enable-gpl \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-pic \
  --enable-shared
  make -j32
  
}

download_and_install ${FFMPEG_URL}